home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Processing a structure- how?
- Date: 26 Feb 1996 13:19:23 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gt84rINN21p@keats.ugrad.cs.ubc.ca>
- References: <DnDv98.Ko1@network.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <DnDv98.Ko1@network.com>,
- Mike Collins <collim@anubis.network.com> wrote:
- >Folks,
- >
- >I want to write a generic routine that will print data contained
- >in structures on the screen (of a DOS-based PC, but I don't think that
- >comes into it). What I want is to be able to pass a structure or
- >structure pointer to the routine, which will then be able to find out what
- >the contents of the structure are, so that if I pass a customer structure
- >containing surname, forename, address, phone number, I will be able to
- >print this. If I pass a different structure containing catalog data, I
- >will be able to print Item, category, bin location, etc.
- >
- >Is there a way for a function to find out how many fields are in a
- >structure and what kind of variable each is (int, char[], etc)? I reallise
- >that this information is known to the compiler and not to the compiled
- >program as such, but can the compiler be made to embed information of this
- >kind into a program?
-
- Yes and no. You need to develop your own system of augmenting a structure
- declaration with additional info that can be used by special routines.
-
- One way is to invent a data definition language, and write a data definition
- compiler. You write your data object specifications in this language, and the
- definition compiler will generate C language headers containing the requisite
- struct declarations, and will perhaps declare some sort of meta-information
- structures that will tell certain library routines extra information about the
- structure fields. The compiler can even automatically generate C code for
- filter routines for performing certain structure-specific operations on the
- data (such as file input or output).
-
- >The effect would be like a printf() statement (I don't mind sending it a
- >formatting string), but with only a structure or structure pointer as a
- >data argument.
-
- An automatically generated filter routine would do this for you. The
- challenging part is writing the processor. A robust data definition compiler
- would parse a language that allows nested structure definitions.
-
- I have worked with a number of such programs, designed for various purposes.
- Sun's ``rpcgen'' interface compiler will generate C header files and
- client/server skeleton/stub code from structure and function definitions. You
- fill in the blanks and have a client/server system working. (SunSoft's latest
- project, the Spring operating system, has a new interface compiler for
- language-independent OO client/server programming). The rpcgen compiler
- generates filter routines, for each structure, for serializing it into an
- external, machine-independent representation, and for deserializing back from
- that representation. A recent project of mine relies heavily on this function
- of rpcgen---we have ported the XDR library to Windows/Winsock for the purpose
- to support client software on that platform.
-
- I've also worked with some database toolkits with such generators; for example
- the Typhoon library, which uses the data definition compiler to generate a
- description of records, fields and their relationships which the database
- engine understands. The database engine can then work with the resulting raw C
- structures, which are directly written to the indexed database files. It's
- fast, but I would not recommend this product except as an example of what you
- can do with a little automatically-generated C.
-
- There is probably freely available software that will do what you are looking
- for, by the way, so unless you are skilled in the use of compiler construction
- tools and can whip this thing out in less than a day, I'd do a little browsing
- first.
- --
-
-